﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

using SBPweb.Localization;

using SBPweb.Contracts;
using SBPweb.Modules.Smartparts;

using SBPweb.Presentation.Windows.Forms.Controls;

namespace SBPweb.Presentation.Windows.Forms.Host.Controls
{
    public partial class MenuControl : MenuToolStrip
	{

		#region Constructor logic

		public MenuControl()
        {
            InitializeComponent();


            ResizeStrip(ToolStripSizes.Small);
            tsmiFile.Image = GetSizedImageFromIcon(Properties.Resources.File, ToolStripSizes.Small);
            tsmiHelp.Image = GetSizedImageFromIcon(Properties.Resources.Help, ToolStripSizes.Small);
            (this as ToolStrip).ItemAdded += new ToolStripItemEventHandler(MenuControl_ItemAdded);

            //MenuControl_Resize(this, new ToolStripSizeChangeEventArgs(ToolStripSizes.Small, 25));
            tsmiLogOff.Click += new EventHandler(tsmiLogOff_Click);
            tsmiExit.Click += new EventHandler(tsmiExit_Click);
            tsmiAbout.Click += new EventHandler(tsmiAbout_Click);
            tsmiShutDown.Click += new EventHandler(tsmiShutDown_Click);
            tsmiRun.Click += new EventHandler(tsmiRun_Click);

            if (Program.IsInExplorerMode)
            {
                tsmiExit.Enabled = false;
            }
            else
            {
#if !DEBUG
                tsmiLogOff.Enabled = false;
                tsmiShutDown.Enabled = false;
                tsmiRun.Enabled = false;
#endif
            }
            //Process

			LocalizationExtender.OnInvalidatingControls += new EventHandler(LocalizationExtender_OnInvalidatingControls);

            //Renderer = new ToolStripProfessionalRenderer(new SBPweb.Themes.Windows.Schemes.CustomColorTable(SBPweb.Themes.Windows.Schemes.Scheme.Verety));
		}

        void MenuControl_ItemAdded(object sender, ToolStripItemEventArgs e)
        {
            if (e.Item.Image != null)
            {
                e.Item.Image = GetSizedImageFromImage(e.Item.Image, ToolStripSizes.Small);
            }
        }

        #endregion

		#region Event handlers

		private void tsmiAbout_Click(object sender, EventArgs e)
        {
            MainForm mf = Program.Loader.Host as MainForm;
            mf.ShowAboutBox();
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
#if !DEBUG
            DialogResult dr = SBPweb.Controls.Windows.Common.MessageBox.Show("Do you really want to exit the application?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dr == DialogResult.Yes)
            {
                MainForm mf = Program.Loader.Host as MainForm;
                mf.Terminate();
            }
#else
            MainForm mf = Program.Loader.Host as MainForm;
            mf.Terminate();
#endif
        }

		private void tsmiRun_Click(object sender, EventArgs e)
        {
            ProcessStartInfo ProcessInfo;
            Process Process;

            ProcessInfo = new ProcessStartInfo("cmd.exe");
            ProcessInfo.CreateNoWindow = false;
            ProcessInfo.UseShellExecute = false;
            Process = Process.Start(ProcessInfo);
        }

		private void tsmiLogOff_Click(object sender, EventArgs e)
        {
            DialogResult dr = SBPweb.Controls.Windows.Common.MessageBox.Show(this.FindForm(), LocalizationExtender.GetString("msgbLogOffText"), LocalizationExtender.GetString("msgbLogOffCaption"), MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (dr == DialogResult.OK)
            {
                SBPweb.Security.User32.ShutDownManager.LogOff();
            }
        }

		private void tsmiShutDown_Click(object sender, EventArgs e)
        {
            DialogResult dr = SBPweb.Controls.Windows.Common.MessageBox.Show(this.FindForm(), LocalizationExtender.GetString("msgbShutDownText"), LocalizationExtender.GetString("msgbShutDownCaption"), MessageBoxButtons.OKCancel, MessageBoxIcon.Stop);
            if (dr == DialogResult.OK)
            {
                SBPweb.Security.User32.ShutDownManager.PowerOff();
            }
        }

		private void LocalizationExtender_OnInvalidatingControls(object sender, EventArgs e)
		{
			this.tsmiExit.Text = LocalizationExtender.GetString("tsmiExitText");
			this.tsmiLogOff.Text = LocalizationExtender.GetString("tsmiLogOffText");
			this.tsmiShutDown.Text = LocalizationExtender.GetString("tsmiShutDownText");
			this.tsmiRun.Text = LocalizationExtender.GetString("tsmiRunText");
			this.tsmiFile.Text = LocalizationExtender.GetString("tsmiFileText");
			this.tsmiAbout.Text = LocalizationExtender.GetString("tsmiAboutText");
			this.tsmiHelp.Text = LocalizationExtender.GetString("tsmiHelpText");
		}

		#endregion

	}
}
